/* eslint-disable id-length */

export const downloadFile = (blob, name) => {
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.style.display = "none";
  a.href = url;
  a.download = name;
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  URL.revokeObjectURL(url);
};
export const scrollNextEvent = (event, config, callback) => {
  if (Math.ceil(event.currentTarget.scrollTop + event.currentTarget.clientHeight) >= event.currentTarget.scrollHeight - 30) {
    if (!config.isEnd && !config.loading) {
      config.params.page++;
      config.loading = true;
      callback();
    }
  }
};

export const nextPageFun = async (api, list, nextPage, config, dataFun) => {
  if (!nextPage) {
    config.params.page = 1;
    list.value = [];
  }
  api.params(config.params);
  await api.send();
  const {data: res} = await api.getDatas();
  let data = res;
  if (dataFun) {
    data = dataFun(data);
  }
  console.log(data);
  if (nextPage) {
    list.value.push(...data.data);
  } else {
    list.value = data.data;
  }
  config.loading = false;
  config.isEnd = config.params.page >= data.last_page;
};

/**
 * Date对象转换工具
 * d -> Date  需要转换的Date对象
 * sp -> Array(2)  日期时间分隔符，sp[0] = 日期分隔符，sp[1] = 时间分隔符
 * return {..., add}  add: 时间计算，参数为需要增加的时间（负数为减少的时间），例如计算今天之后2天10小时的时间，需要参数{day: 2, hour: 10}
 */
export const dateTransform = (d = new Date(), conf = {sp: ["-", ":"]}) => {
  // 初始化
  let sp = conf.sp;
  // 转换字符串类型
  if (typeof (d) === "string") {
    d = new Date(d);
  }
  // 初始化基本数据
  let y = d.getFullYear();
  let m = d.getMonth() + 1;
  let _d = d.getDate();
  let h = d.getHours();
  let _m = d.getMinutes();
  let s = d.getSeconds();
  let w = d.getDay();
  let _m_ = m.toString().padStart(2, "0");
  let __d_ = _d.toString().padStart(2, "0");
  let _h_ = h.toString().padStart(2, "0");
  let __m_ = _m.toString().padStart(2, "0");
  let _s_ = s.toString().padStart(2, "0");
  let timestamp = d.getTime();
  // 日常时间与问候语
  let daily = [];
  if (h >= 0) {
    daily = ["凌晨", "夜深了，注意休息"];
  }
  if (h >= 5) {
    daily = ["上午", "上午好"];
  }
  if (h >= 10) {
    daily = ["中午", "中午好"];
  }
  if (h >= 12) {
    daily = ["中午", "现在是午休时间，注意休息"];
  }
  if (h >= 13) {
    daily = ["下午", "下午好"];
  }
  if (h >= 19) {
    daily = ["晚上", "晚上好"];
  }
  if (h >= 22) {
    daily = ["深夜", "夜深了，注意休息"];
  }
  // 时间差
  let difference = (diff = new Date()) => {
    if (typeof (diff) === "string") {
      diff = new Date(diff);
    }
    diff = new Date(timestamp - diff.getTime());
    return {
      year: diff.getFullYear() - 1970,
      month: diff.getMonth(),
      day: diff.getDate() - 1,
      hour: diff.getHours(),
      minute: diff.getMinutes(),
      second: diff.getSeconds(),
    };
  };
  // 时间计算
  let add = (desc = {}) => {
    let msecond = 0;
    if (desc.year) {
      msecond += desc.year * 365 * 24 * 3600 * 1000;
    }
    if (desc.month) {
      msecond += desc.month * 31 * 24 * 3600 * 1000;
    }
    if (desc.day) {
      msecond += desc.day * 24 * 3600 * 1000;
    }
    if (desc.hour) {
      msecond += desc.hour * 3600 * 1000;
    }
    if (desc.minute) {
      msecond += desc.minute * 60 * 1000;
    }
    if (desc.second) {
      msecond += desc.second * 1000;
    }
    // eslint-disable-next-line no-undef
    return dateTransform(new Date(d.getTime() + msecond), {sp});
  };

  return {
    dateObject: d,
    year: y,
    month: m,
    day: _d,
    hour: h,
    minute: _m,
    second: s,
    datetime: `${y}${sp[0]}${_m_}${sp[0]}${__d_} ${_h_}${sp[1]}${__m_}${sp[1]}${_s_}`,
    date: `${y}${sp[0]}${_m_}${sp[0]}${__d_}`,
    time: `${_h_}${sp[1]}${__m_}${sp[1]}${_s_}`,
    monthDate: `${y}${sp[0]}${_m_}`,
    dayDate: `${_m_}${sp[0]}${__d_}`,
    hourTime: `${_h_}${sp[1]}${__m_}`,
    minuteTime: `${__m_}${sp[1]}${_s_}`,
    weekday: w === 0 ? 7 : w,
    weekdayName: [`周${["日", "一", "二", "三", "四", "五", "六"][w]}`, `星期${["日", "一", "二", "三", "四", "五", "六"][w]}`],
    timestamp,
    daily,
    difference,
    add,
  };
};
